package cn.hr.service.impl;

import cn.hr.dao.mapper.DataAnalysisMapper;
import cn.hr.dao.mapper.DictionaryMapper;
import cn.hr.pojo.Dictionary;
import cn.hr.service.DataAnalysisService;
import cn.hr.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MO on 2019/1/14.
 */
@CacheConfig(cacheNames = "archives")
@Scope("prototype")
@Service("dataAnalysisService")
public class DataAnalysisServiceImpl implements DataAnalysisService {
    @Resource
    private DataAnalysisMapper dataAnalysisMapper;
    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public PageInfo<Map> findArchivesByCondition(Map map, int pageNo) throws Exception {
        PageHelper.startPage(pageNo, Constants.PAGESIZE);
        List<Map> list = dataAnalysisMapper.findArchivesByCondition(map);
        return new PageInfo<Map>(list);
    }

    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public Map findConsolidatedStaffDataReport() throws Exception {
        Map alert = new HashMap();
        List<Map> resultList = new ArrayList<Map>();
        Map result = null;
        List<Dictionary> educationList = dictionaryMapper.selectAllByTypeName("EDUCATION_TYPE");
        List<Map> list = dataAnalysisMapper.findConsolidatedStaffDataReport();
        long sum = 0;
        int year = 0;
        List<Map> resultEducation = listEachList(educationList, true);
        alert.put("educations", resultEducation);
        year = (int) list.get(0).get("year");
        for (int i = 0; i < list.size(); i++) {
            if (year == (int) list.get(i).get("year")) {
                for (Map education : resultEducation) {
                    if ((int) list.get(i).get("education") == (int) education.get("id")) {
                        education.put("counto", list.get(i).get("counto"));
                        break;
                    }
                }
                sum += (long) list.get(i).get("counto");
            } else {
                result = new HashMap();
                result.put("year", year);
                result.put("sum", sum);
                result.put("education", resultEducation);
                resultList.add(result);
                year = (int) list.get(i).get("year");
                sum = 0;
                resultEducation = listEachList(educationList, false);
                for (Map education : resultEducation) {
                    if ((int) list.get(i).get("education") == (int) education.get("id")) {
                        education.put("counto", list.get(i).get("counto"));
                        break;
                    }
                }
                sum += (long) list.get(i).get("counto");
            }
            if (i == list.size() - 1) {
                result = new HashMap();
                result.put("year", year);
                result.put("sum", sum);
                result.put("education", resultEducation);
                resultList.add(result);
            }
        }
        alert.put("list", resultList);
        return alert;
    }

    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public Map findConsolidatedStaffDataReportO(String year) throws Exception {
        Map alert = new HashMap();
        List<Map> resultList = new ArrayList<Map>();
        Map result = null;
        List<Dictionary> educationList = dictionaryMapper.selectAllByTypeName("EDUCATION_TYPE");
        List<Map> list = dataAnalysisMapper.findConsolidatedStaffDataReportO(year);
        long sum = 0;
        String oName;
        List<Map> resultEducation = listEachList(educationList, true);
        alert.put("educations", resultEducation);
        oName = (String) list.get(0).get("oName");
        for (int i = 0; i < list.size(); i++) {
            if (oName.equals(list.get(i).get("oName"))) {
                for (Map education : resultEducation) {
                    if ((int) list.get(i).get("education") == (int) education.get("id")) {
                        education.put("counto", list.get(i).get("counto"));
                        break;
                    }
                }
                sum += (long) list.get(i).get("counto");
            } else {
                result = new HashMap();
                result.put("oName", oName);
                result.put("sum", sum);
                result.put("education", resultEducation);
                resultList.add(result);
                oName = (String) list.get(i).get("oName");
                sum = 0;
                resultEducation = listEachList(educationList, false);
                for (Map education : resultEducation) {
                    if ((int) list.get(i).get("education") == (int) education.get("id")) {
                        education.put("counto", list.get(i).get("counto"));
                        break;
                    }
                }
                sum += (long) list.get(i).get("counto");
            }
            if (i == list.size() - 1) {
                result = new HashMap();
                result.put("oName", oName);
                result.put("sum", sum);
                result.put("education", resultEducation);
                resultList.add(result);
            }
        }
        alert.put("list", resultList);
        return alert;
    }

    private List<Map> listEachList(List<Dictionary> educationList, boolean fend) {
        List<Map> resultEducation = new ArrayList<Map>();
        for (Dictionary dic : educationList) {
            Map educationMap = new HashMap();
            educationMap.put("id", dic.getId());
            if (fend) {
                educationMap.put("valueName", dic.getValueName());
                educationMap.put("counto", 0);
            } else {
                educationMap.put("counto", 0);
            }
            resultEducation.add(educationMap);
        }
        return resultEducation;
    }
}
